// pages/details/index.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: wx.getStorageSync('isLogin') ? wx.getStorageSync('isLogin') : false,
        // 大类型: 1-项目预测, 2-工作事业选择, 3-情感咨询, 4-公关审核
        type: 1,
        dataList: [], //数据列表
        currentId: 0,
        clickType: 1,
        title: '',
        message: '',
        show: false,
        // 用户选择, 大类型下的,具体某个小选项
        itype: 0,
        // 工作事业选择, 职位内容
        jobTitle: '',
        // 选择的问题
        chooseQuestion: '',
        zifei: 0,
        freeTimes: 0,
        isFree: 0,
        payShow: false,
        theme:'',
        ffbtn:'',
        mfbtn:'',
        wode:'',
        indexpng:''
    },   
    getPhoneNumber(e) {
        console.log(e);
        let {
            encryptedData,
            iv,
            code
        } = e.detail
        // 如果授权获取手机号则进入下个页面 
        if (code) {
            this.getPhone(code)
        } else {
            Dialog.alert({
                title: '温馨提示',
                message: '请授权登录',
                theme: 'round-button',
            }).then(() => {
                // on close
            });
        }
    },

    // 用户获取手机号
    getPhone(code) {
        let that = this
        var reqUri = app.globalData.domain + 'index/index/getUserWxPhone';
        wx.request({
            url: reqUri,
            method: 'POST',
            data: {
                code,
            },
            success(res) {
                console.log(res)
                if (res.data.code == app.globalData.successCode) {
                    let {
                        phoneNumber
                    } = res.data.data
                    // 将用户的 手机号缓存
                    wx.setStorageSync('user_phone', res.data.data)
                    that.login(phoneNumber)
                } else {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg
                    })
                    return ;                    
                }
            }
        })
    },

    // 用户登录
    login(phone) {
        let that = this
        var reqUri = app.globalData.domain + 'index/index/doReg';
        var uid =  wx.getStorageSync('toid')??0;
        let cid3 = wx.getStorageSync('tocid3');

        if (!cid3 || cid3 == undefined) cid3 = that.data.cid3;
        wx.request({
            url: reqUri,
            method: 'POST',
            data: {
                phone,
                types: 'mini',
                openid: app.globalData.openId,
                uid:uid,
                cid3:cid3,
                agentId: wx.getStorageSync('agentId') ?? 1
            },
            success(res) {
                console.log(res)
                if (res.data.code == app.globalData.successCode) {
                    Toast.success('登录成功,即将跳转')
                    wx.setStorageSync('isLogin', true)
                    wx.redirectTo({
                      url: '/pages/personal/me',
                  })
                } else {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg
                    })
                    return ;                    
                }
            }
        })
    },
    /**
     * 用户输入咨询的问问题
     */
    changeMsg(e) {
        this.setData({
            message: e.detail.value
        });
    },
    /**
     * 用户输入职位
     */
    changeJob(e) {
        this.setData({
            jobTitle: e.detail.value
        });
    },
    /**
     * 选择咨询--打开
     */
    showPopup() {
        this.setData({
            show: true
        });
    }, 
    /**
     * 选择咨询--关闭
     */    
    onClose() {
        this.setData({
            show: false
        });
    },
    /**
     * 弹出套餐层
     */
    showCombo: function() {
        this.setData({
            payShow: true
        });
    },
    /**
     * 隐藏套餐层
     */
    closeCombo: function() {
        this.setData({
            payShow: false
        });
    },    
    /**
     * 选择 xx菜单下, 具体咨询类目
     */
    chooseDetail(e) {
        console.log(e);
        var itype = e.currentTarget.dataset.itype;
        var chooseQuestion = e.currentTarget.dataset.title;
        this.setData({
            itype: itype,
            show: false,
            chooseQuestion: chooseQuestion
        });        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        let {
            type
        } = options
        this.setData({
            type,
            openId: app.globalData.openId
        });
        // 判断当前用户, 是否可以免费次数
        var cacheUserInfo = wx.getStorageSync('user_phone');
        var that = this;

        wx.request({
            url: app.globalData.domain + 'v3/index/v2getUserIsUse',
            data: {
                user_phone: cacheUserInfo.phoneNumber,
                type: type
            },
            success (res) {
                console.log(res.data.data.is_used)
                if (res.data.code == 0) {
                    that.setData({
                        zifei: res.data.data.zifei,
                        freeTimes: res.data.data.times,
                        isFree: res.data.data.is_used              
                    });
                }
            }
        })        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if(wx.getStorageSync('theme') == 'style1'){
            this.setData({
              theme:'style1',
              ffbtn:'https://aiw.oss-cn-beijing.aliyuncs.com/year/Group15.png',
              mfbtn:'https://aiw.oss-cn-beijing.aliyuncs.com/year/Group16.png',
              wode:'/images/wodepng.png',
              indexpng:'/images/indexpng.png'
            })
        }else{
            this.setData({
              theme:'init',
              ffbtn:'https://aiw.oss-cn-beijing.aliyuncs.com/v3/-s-ks_ff.png',
              mfbtn:'https://aiw.oss-cn-beijing.aliyuncs.com/v3/ks_mf@2x.png',
              wode:'https://aiw.oss-cn-beijing.aliyuncs.com/v3/tab_me_n@2x.png',
              indexpng:'https://aiw.oss-cn-beijing.aliyuncs.com/v3/tab_home_n@2x(1).png'
            })
        }
        this.setData({
            isLogin: wx.getStorageSync('isLogin')
          })
        setTimeout(() => {
            app.show(this, 'show', 1)
        }, 1000);
    },
    onHide: function () {
        setTimeout(() => {
            app.show(this, 'show', 0)
        }, 1000);
    },
    /**
     * 免费体验的测试
     */
    freeLoading() {
        let types = this.data.type
        // 必须要选择 咨询类目
        if (!this.data.message && !this.data.jobTitle) {
            Toast('必须选择咨询类目');
            return ;            
        }
        if (this.data.message.length > 255) {
            Toast('咨询字数不超过255个字符');
            return ;
        }
        if (this.data.jobTitle.length > 100) {
            Toast('职位描述,字数不超过100个字符');
            return ;
        }        

        wx.setStorageSync('type', types);
        wx.setStorageSync('itype', this.data.itype);
        wx.setStorageSync('message', this.data.message);
        wx.setStorageSync('person_job', this.data.jobTitle);
        // 弹窗提示
        wx.showModal({
            title: '温馨提示',
            content: '请再次确认您的输入内容',
            success (res) {
                if (res.confirm) {
                    wx.navigateTo({
                        url: '/pages/loading/loading?types=' + types,
                    })
                }
            }
        })    
    },
    /**
     * 开始测试
     */
    toLoading(e) {
        let types = this.data.type;
        // 必须要选择 咨询类目
        if (!this.data.message && !this.data.jobTitle) {
            Toast('必须选择咨询类目');
            return ;            
        }
        if (this.data.message.length > 255) {
            Toast('咨询字数不超过255个字符');
            return ;
        }
        if (this.data.jobTitle.length > 100) {
            Toast('职位描述,字数不超过100个字符');
            return ;
        }      
        var that = this;  
        // 弹窗提示
        wx.showModal({
            title: '温馨提示',
            content: '请再次确认您的输入内容',
            success (res) {
                if (res.confirm) {
                    wx.setStorageSync('type', types);
                    wx.setStorageSync('itype', that.data.itype);
                    wx.setStorageSync('message', that.data.message);
                    wx.setStorageSync('person_job', that.data.jobTitle);
                    // 发起支付
                    // 套餐类型,1-月卡,0-次卡套餐
                    var comboType = e.currentTarget.dataset.combo_type;
                    wx.setStorageSync('combo_type', comboType);
                    // 获取用户信息缓存
                    var cacheUserInfo = wx.getStorageSync('user_phone');
                    
                    wx.request({
                      url: app.globalData.domain + 'v3/index/intoOrder',
                      data: {
                            type: types,
                            combo_type: comboType,
                            user_phone: (cacheUserInfo.phoneNumber && typeof (cacheUserInfo.phoneNumber) != 'undefined') ? cacheUserInfo.phoneNumber : ''
                        },
                        success: function (res) {
                            console.log(res)
                            if (res.data.code != 0) {
                                wx.showModal({
                                    title: '温馨提示',
                                    showCancel: false,
                                    content: res.data.msg
                                })
                                return ;
                            }
                            // 发起支付
                            wx.requestPayment({
                                timeStamp: res.data.data.timeStamp,
                                nonceStr: res.data.data.nonceStr,
                                package: res.data.data.package,
                                signType: res.data.data.signType,
                                paySign: res.data.data.paySign,
                                success (res) {
                                    console.log('debug支付成功');
                                    wx.navigateTo({
                                        url: '/pages/loading/loading?types=' + types,
                                    })                        
                                },
                                fail (res) {
                                    wx.showModal({
                                        title: '温馨提示',
                                        showCancel: false,
                                        content: '支付失败!'
                                    })
                                    return ;                        
                                }
                            })                
                        }          
                    })
                }
            }            
        })        
    },
    // 点击确定
    onConfirm() {
        let {
            types
        } = this.data.currentItem
        let xintai = 'choseInfo.xintai'
        let initiative = 'choseInfo.initiative'
        let is_help = 'choseInfo.is_help'
        if (types == 1) {
            this.setData({
                [xintai]: this.data.currentItem,
            })
        } else if (types == 2) {
            this.setData({
                [initiative]: this.data.currentItem,
            })
        } else {
            this.setData({
                [is_help]: this.data.currentItem,
            })
        }
        console.log(this.data.choseInfo)
    },
    checkIsLogin() {
        var reqUri = app.globalData.domain + 'index/index/index?types=';
        wx.request({
            url: reqUri,
            success(res) {
                if (res.data.code == app.globalData.successCode) {
                    that.setData({
                        id: res.data.data.id,
                        num_sn: res.data.data.num_sn,
                        result: res.data.data.result,
                        tuijin: res.data.data.tuijin,
                        current: res.data.data.current,
                        xintai: res.data.data.xintai,
                        solution: res.data.data.solution,
                        idea: res.data.data.idea
                    });
                }
            }
        })
    },
    /**
     * 至首页
     */
    toHome() {
        wx.navigateTo({
            url: '/pages/menu/index',
        })
    },
    /**
     * 至个人中心
     */
    toMe() {
        wx.navigateTo({
            url: '/pages/personal/me',
        })
    }
})